package com.polaris.live.ui.live.proxy.caller

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.databinding.FragmentCallLiveVideoBinding
import com.polaris.live.resp.back_resp.CallCheckResp
import com.polaris.live.utils.image.ImageUtils

/**
 * CallVideoListenerProxy
 *
 * @author Created by 半仙 on 2023/9/26/026 15:07
 */
class CallVideoListenerProxy(
    private val context: Context,
    private val binding: FragmentCallLiveVideoBinding
) : BaseVideoListenerProxy() {

    /**
     * 缓存已生成的LinearLayout
     */
    private val linearLayouts: MutableList<LinearLayout> = mutableListOf()

    fun onCheckCallBackChange(callCheckResp: CallCheckResp) {
        if (callCheckResp.free) {
            binding.callFreeImg.visibility = View.VISIBLE
        } else {
            binding.callFreeImg.visibility = View.GONE
        }
        binding.callLeftTopLl.removeAllViews()
        callCheckResp.messages.forEachIndexed { index, message ->
            val linearLayout = linearLayouts.getOrNull(index) ?: creatorLinearLayout(context).apply {
                linearLayouts.add(this)
            }
            linearLayout.apply {
                val image = linearLayout.findViewById<ImageView>(R.id.videolistenerproxy_img)
                Glide.with(image.context).load(ImageUtils.imgPath(message.pic)).into(image)
                val textView = linearLayout.findViewById<TextView>(R.id.videolistenerproxy_tv)
                textView.text = message.content
            }
            binding.callLeftTopLl.addView(linearLayout)
        }
    }
}